﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace HYFamilyLibDataManager
{
	public partial class DlgCompare : System.Windows.Forms.Form
	{
		public List<CategoryForSelect> CategoryList
		{
			get
			{
				List<CategoryForSelect> list = new List<CategoryForSelect>();
				this.getChildNodes(this.tvCategory.Nodes[0], list);
				return list;
			}
		}

		private void getChildNodes(TreeNode node, List<CategoryForSelect> categoryList)
		{
			foreach (object obj in node.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Tag is CategoryForSelect)
				{
					categoryList.Add((CategoryForSelect)treeNode.Tag);
				}
				foreach (object obj2 in treeNode.Nodes)
				{
					TreeNode treeNode2 = (TreeNode)obj2;
					if (treeNode2.Tag is CategoryForSelect)
					{
						categoryList.Add((CategoryForSelect)treeNode2.Tag);
					}
					this.getChildNodes(treeNode2, categoryList);
				}
			}
		}

		public DlgCompare(FamilyLibDatabase tagDb, FamilyLibDatabase newDb)
		{
			this.mTagDb = tagDb;
			this.mNewDb = newDb;
			this.InitializeComponent();
		}

		private void DlgCompare_Load(object sender, EventArgs e)
		{
			this.mTagDb.Compare(this.mNewDb, this.tvCategory);
		}

		private void tvCategory_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (this.tvCategory.SelectedNode.Tag is CategoryForSelect)
			{
				this.lbxFamily.Items.Clear();
				foreach (FamilyForSelect item in (this.tvCategory.SelectedNode.Tag as CategoryForSelect).FamilyList)
				{
					this.lbxFamily.Items.Add(item, true);
				}
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
		}

		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.cmdSearch();
		}

		private void cmdSearch()
		{
			this.lbxFamily.Items.Clear();
			List<DbFamily> list = this.mNewDb.FamilyTable.SearchFamily(this.tstbSearchName.Text);
			if (list.Count > 0)
			{
				foreach (DbFamily item in list)
				{
					this.lbxFamily.Items.Add(item, true);
				}
			}
			this.lbxFamily.Focus();
		}

		private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetSelected(i, true);
			}
		}

		private void 反选ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetSelected(i, !this.lbxFamily.GetItemChecked(i));
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetItemChecked(i, true);
			}
		}

		private void btnSelChange_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetItemChecked(i, !this.lbxFamily.GetItemChecked(i));
			}
		}

		private void lbxFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void lbxFamily_ItemCheck(object sender, ItemCheckEventArgs e)
		{
			if (this.lbxFamily.Items[e.Index] != null && this.lbxFamily.Items[e.Index] is FamilyForSelect)
			{
				FamilyForSelect familyForSelect = this.lbxFamily.Items[e.Index] as FamilyForSelect;
				CategoryForSelect category = familyForSelect.Category;
				if (familyForSelect != null)
				{
					if (e.NewValue == CheckState.Checked)
					{
						using (List<FamilyForSelect>.Enumerator enumerator = category.FamilyList.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								FamilyForSelect familyForSelect2 = enumerator.Current;
								if (familyForSelect2.ToString() == familyForSelect.ToString())
								{
									familyForSelect2.Selected = true;
									break;
								}
							}
							return;
						}
					}
					foreach (FamilyForSelect familyForSelect3 in category.FamilyList)
					{
						if (familyForSelect3.ToString() == familyForSelect.ToString())
						{
							familyForSelect3.Selected = false;
							break;
						}
					}
				}
			}
		}

		private void lbxFamily_MouseClick(object sender, MouseEventArgs e)
		{
			if (this.lbxFamily.SelectedItem != null && ((ListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.lbxFamily.ClearSelected();
			}
		}

		private void DlgCompare_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void lbxFamily_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.lbxFamily.SelectedItem != null && ((ListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.lbxFamily.ClearSelected();
			}
		}

		private FamilyLibDatabase mTagDb;

		private FamilyLibDatabase mNewDb;
	}
}
